package com.ruoyi.module.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.module.service.IDictWordImportService;

/**
 * 词典单词导入Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/module/dict/word/import")
public class DictWordImportController extends BaseController
{
    @Autowired
    private IDictWordImportService dictWordImportService;

    /**
     * 导入指定词典的单词
     */
    @PreAuthorize("@ss.hasPermi('module:dict:book:add')")
    @Log(title = "导入词典单词", businessType = BusinessType.IMPORT)
    @PostMapping("/{bookId}")
    public AjaxResult importWords(@PathVariable String bookId)
    {
        // 默认词典路径
        String dictPath = "/root/workspace/3.1.0-dev/work/aiword/public/dicts/";
        
        // 需要从数据库获取词典信息，构造完整文件路径
        // 这里简化处理，假设词典的url字段已经包含了文件名
        String jsonFilePath = dictPath + bookId + ".json";
        
        return dictWordImportService.importWordsFromJson(bookId, jsonFilePath, getUsername());
    }

    /**
     * 从JSON内容导入单词（用于用户自定义词典）
     */
    @PreAuthorize("@ss.hasPermi('module:dict:book:add')")
    @Log(title = "导入自定义词典单词", businessType = BusinessType.IMPORT)
    @PostMapping("/content/{bookId}")
    public AjaxResult importWordsFromContent(@PathVariable String bookId, @RequestBody String jsonContent)
    {
        return dictWordImportService.importWordsFromJsonContent(bookId, jsonContent, getUsername());
    }

    /**
     * 批量导入所有词典的单词
     */
    @PreAuthorize("@ss.hasPermi('module:dict:book:add')")
    @Log(title = "批量导入词典单词", businessType = BusinessType.IMPORT)
    @PostMapping("/batch")
    public AjaxResult batchImportAll()
    {
        String dictPath = "/root/workspace/3.1.0-dev/work/aiword/public/dicts/";
        return dictWordImportService.batchImportAllDictWords(dictPath, getUsername());
    }

    /**
     * 清空指定词典的所有单词
     */
    @PreAuthorize("@ss.hasPermi('module:dict:book:remove')")
    @Log(title = "清空词典单词", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bookId}")
    public AjaxResult clearWords(@PathVariable String bookId)
    {
        int count = dictWordImportService.deleteWordsByBookId(bookId);
        return AjaxResult.success("成功删除 " + count + " 个单词");
    }
}

